﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;

namespace Metro.Kashikoi.ServiceContract.Location
{
    /// <summary>
    /// This class defines the facility.
    /// </summary>
    [DataContract(IsReference = true)]
    public class Facility : Region
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Facility"/> class.
        /// </summary>
        public Facility()
        {
            Address = new Address();
        }

        /// <summary>
        /// Gets or sets the address.
        /// </summary>
        /// <value>
        /// The address.
        /// </value>
        [DataMember]
        public virtual Address Address { get; set; }

        /// <summary>
        /// Gets or sets the windows group.
        /// </summary>
        /// <value>
        /// The windows group.
        /// </value>
        [DataMember]
        public string WindowsGroup { get; set; }

        /// <summary>
        /// Gets or sets the organization.
        /// </summary>
        /// <value>
        /// The organization.
        /// </value>
        [NotMapped]
        public Organization Organization
        {
            get { return this.Parent as Organization; }
            set { this.Parent = value; }
        }

        /// <summary>
        /// Gets or sets the units.
        /// </summary>
        /// <value>
        /// The units.
        /// </value>
        [NotMapped]
        public IEnumerable<Unit> Units
        {
            get
            {
                if (this.Children != null)
                {
                    return this.Children.OfType<Unit>();
                }
                return null;
            }
            set
            {
                if (this.Children == null)
                {
                    this.Children = new List<Region>();
                }
                foreach (var item in value)
                {
                    this.Children.Add(item);
                }
            }
        }
    }
}
